/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * NotificationWindow.java
 *
 * Created on Apr 18, 2010, 12:11:53 PM
 */
package com.sowrov.gui.window;

import com.sowrov.afrigis.tools.app.timesheet_manager.TaskManagerView;
import com.sowrov.util.logsystem.ChronicleLogger;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Random;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;

/**
 *
 * @author isearch1
 */
public class NotificationWindow extends javax.swing.JFrame {

    /** Creates new form NotificationWindow */
    private NotificationWindow self;

    private TaskManagerView parent;

    private Random random;

    public NotificationWindow() {
    }

    /**
     * NotificationWindow Constructor
     * 
     * @param parent TaskManagerView
     */
    public NotificationWindow(final TaskManagerView parent) {
        this.setUndecorated(true);
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        initComponents();
        this.parent = parent;
        this.setAlwaysOnTop(true);
        this.goButton.requestFocus();
        if (this.parent != null) {
            this.setIconImage(this.parent.getIcon());
        }
        this.random = new Random(new Date().getTime());
        //this will return maximum window size excluding status bar height
        Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        int windowHeight = (int) maxBounds.getHeight();
        int windowWidth = (int) maxBounds.getWidth();

        this.setLocation(windowWidth - this.getWidth(), windowHeight - this.getHeight());

        try {
           Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
           Method mSetWindowOpcure = awtUtilitiesClass.getMethod("setWindowOpaque", Window.class, boolean.class);
           mSetWindowOpcure.invoke(null, this, false);
           Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
           mSetWindowOpacity.invoke(null, this, Float.valueOf(1.0f));
        } catch (NoSuchMethodException ex) {
           ex.printStackTrace();
        } catch (SecurityException ex) {
           ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
           ex.printStackTrace();
        } catch (IllegalAccessException ex) {
           ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
           ex.printStackTrace();
        } catch (InvocationTargetException ex) {
           ex.printStackTrace();
        }
        

//        if ((AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT)) &&
//            (AWTUtilities.isTranslucencyCapable(defaultTranslucencyCapableGC))) {
//            System.out.println("OK");
//            self = this;
//            self.addComponentListener(new ComponentAdapter() {
//
//                @Override
//                public void componentResized(final ComponentEvent evt) {
//                    Shape shape = null;
//                    //shape = new Ellipse2D.Float(0, 0, self.getWidth(), self.getHeight());
//                    shape = new RoundRectangle2D.Float(0, 0, self.getWidth(), self.getHeight(), 20, 20);
//                    AWTUtilities.setWindowShape(self, shape);
//                }
//            });
//
//        }
//        else {
//            System.out.println("No");
//        }
    }

    /**
     * show notification window with default message
     */
    public void showNotification() {
        this.showNotification(
                "<html><body>You did not update TimeSheet for a while.<br>Do you want to update now?</body></html>");
    }

    public void showNotification(String html) {
        this.textEP.setText(html);
        int index = this.random.nextInt(6);

        ChronicleLogger.getInstance().logDebug(index+"");

        this.titleLabel.setIcon(new javax.swing.ImageIcon(getClass()
                .getResource("/com/sowrov/afrigis/tools/app/timesheet_manager/resources/NotifyBox"+index+".png")));
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textEP = new javax.swing.JEditorPane();
        goButton = new javax.swing.JButton();
        noBUtton = new javax.swing.JButton();
        titleLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(153, 204, 0));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setMinimumSize(new java.awt.Dimension(21, 24));
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setOpaque(false);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(6, 26));

        textEP.setContentType("text/html"); // NOI18N
        textEP.setEditable(false);
        textEP.setFont(new java.awt.Font("Times New Roman", 0, 11));
        textEP.setAutoscrolls(false);
        textEP.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textEP.setFocusCycleRoot(false);
        textEP.setName("textEP"); // NOI18N
        textEP.setOpaque(false);
        textEP.setPreferredSize(new java.awt.Dimension(6, 26));
        jScrollPane1.setViewportView(textEP);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 330, 40));

        goButton.setMnemonic('G');
        goButton.setText("Lets Go!");
        goButton.setName("goButton"); // NOI18N
        goButton.setOpaque(false);
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });
        getContentPane().add(goButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 70, 89, -1));

        noBUtton.setMnemonic('N');
        noBUtton.setText("Ignore");
        noBUtton.setDisplayedMnemonicIndex(1);
        noBUtton.setName("noBUtton"); // NOI18N
        noBUtton.setOpaque(false);
        noBUtton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noBUttonActionPerformed(evt);
            }
        });
        getContentPane().add(noBUtton, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 89, -1));

        titleLabel.setBackground(new java.awt.Color(255, 255, 102));
        titleLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/sowrov/afrigis/tools/app/timesheet_manager/resources/NotifyBox5.png"))); // NOI18N
        titleLabel.setFocusable(false);
        titleLabel.setName("titleLabel"); // NOI18N
        getContentPane().add(titleLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 350, 100));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed
        if (this.parent != null) {
            this.dispose();
            this.parent.bringFront();
        }
    }//GEN-LAST:event_goButtonActionPerformed

    private void noBUttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noBUttonActionPerformed
        this.dispose();
    }//GEN-LAST:event_noBUttonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                NotificationWindow frm = new NotificationWindow(null);
                frm.showNotification();
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton goButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton noBUtton;
    private javax.swing.JEditorPane textEP;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}
